package com.joinway.mobile.controller;

import java.util.List;

import javax.validation.constraints.Min;

import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiBodyObject;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.joinway.framework.bean.logging.annotation.InputLog;
import com.joinway.framework.bean.logging.annotation.OutputLog;
import com.joinway.framework.bean.logging.type.LogFormat;
import com.joinway.framework.bean.view.View;
import com.joinway.framework.core.controller.ExceptionController;
import com.joinway.framework.support.audit.annotation.Audit;
import com.joinway.mobile.bean.Product;
import com.joinway.mobile.bean.form.OrderForm;
import com.joinway.mobile.bean.view.OrderView;
import com.joinway.mobile.bean.view.ProductDetailView;
import com.joinway.mobile.bean.view.ProductView;
import com.joinway.mobile.bean.view.ShopCartView;
import com.joinway.mobile.service.JiayidaService;

@Api(name = "母婴商城接口", description = "母婴商品产品相关接口")
@RestController
@RequestMapping("baby")
@Validated
public class BabyController extends ExceptionController {

	@Autowired @Qualifier("babyService") JiayidaService service;
	
	@ApiMethod(description="获得母婴产品列表", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/product/list.json", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	//test
	public ProductView getProducts(
			@Min(1) @RequestParam("catId") @ApiQueryParam(name="catId", description="产品类型id; 3-限时闪购;7-为宝宝买;8-为我家买;9-为自己买") int catId,
			@Min(1) @RequestParam(value="page",required=false,defaultValue="1") @ApiQueryParam(name="page", required = false, description="页码号，从1开始") int page,
			@Min(1) @RequestParam(value="rows",required=false,defaultValue="2") @ApiQueryParam(name="rows", required = false, description="本页记录个数，默认2条") int rows
	) throws Exception {
		ProductView view = new ProductView();
		
		List<Product> products = service.getProducts(catId, page, rows);
		view.setProducts(products);
		
		return view;
	}

	
	@ApiMethod(description="获得母婴产品详细信息", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/product/detail.json", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public ProductDetailView getProductDetail(@Min(1) @RequestParam("pid") @ApiQueryParam(name="pid", description="产品id") int pid) throws Exception {
		ProductDetailView view = new ProductDetailView();
		
		view.setProduct(service.getProductDetail(pid));
		
		return view;
	}

	@ApiMethod(description="获得已购物产品列表", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/order/list.json", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public OrderView getOrderedProducts(
			@Min(1) @RequestParam("userId") @ApiQueryParam(name="userId", description="用户id") int userId,
			@Min(1) @RequestParam(value="page",required=false,defaultValue="1") @ApiQueryParam(name="page", required = false, description="页码号，从1开始") int page,
			@Min(1) @RequestParam(value="rows",required=false,defaultValue="2") @ApiQueryParam(name="rows", required = false, description="本页记录个数，默认2条") int rows
	) throws Exception {
		OrderView view = new OrderView();
		
		view.setProducts(service.getOrderedProducts(userId, page, rows));
		
		return view;
	}

	@ApiMethod(description="添加商品到购物车", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/cart/add.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public View addProduct(
			@Min(1) @RequestParam("userId") @ApiQueryParam(name="userId", description="用户id") int userId,
			@Min(1) @RequestParam("pid") @ApiQueryParam(name="pid", description="产品id") int pid,
			@RequestParam(value="propId",required=false,defaultValue="") @ApiQueryParam(name="propId", description="逗号分隔的属性id,如1,2,3") String propId,
			@Min(1) @RequestParam(value="number",required=false,defaultValue="1") @ApiQueryParam(name="number", description="购买数量，默认为1") int number
	) throws Exception {
		service.addShopCart(userId, pid, propId, number);
		return new View();
	}

	@ApiMethod(description="修改购物车中商品", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/cart/update.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public View updateProduct(
			@Min(1) @RequestParam("cartId") @ApiQueryParam(name="cartId", description="购物车项目id") int cartId,
			@Min(1) @RequestParam("number") @ApiQueryParam(name="number", description="商品数量") int number
	) throws Exception {
		service.updateShopCart(cartId, number);
		return new View();
	}

	@ApiMethod(description="获得购物车产品列表", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/cart/list.json", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public ShopCartView getShopCartProducts(
			@Min(1) @RequestParam("userId") @ApiQueryParam(name="userId", description="用户id") int userId,
			@Min(1) @RequestParam(value="page",required=false,defaultValue="1") @ApiQueryParam(name="page", required = false, description="页码号，从1开始") int page,
			@Min(1) @RequestParam(value="rows",required=false,defaultValue="2") @ApiQueryParam(name="rows", required = false, description="本页记录个数，默认2条") int rows
	) throws Exception {
		ShopCartView view = new ShopCartView();
		
		view.setProducts(service.getShopCartProduct(userId, page, rows));
		
		return view;
	}

	@ApiMethod(description="删除购物车中商品", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/cart/remove.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public View removeShopCartProduct(
			@Min(1) @RequestParam("cartId") @ApiQueryParam(name="cartId", description="购物车项目id") int cartId
	) throws Exception {
		
		service.deleteShopCart(cartId);
		
		return new View();
	}
	
//	@ApiMethod(description="新增订单记录", produces=MediaType.APPLICATION_JSON_VALUE)
//	@ApiResponseObject
	@RequestMapping(value="/order/addOrder.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	@Deprecated public View addOrder(
			@RequestParam(value="order",required=true) @ApiQueryParam(name="order", description="订单数据") String order,
			@RequestParam(value="goodsList",required=true) @ApiQueryParam(name="goodsList", description="商品列表数据") String goodsList,
			@RequestParam(value="cartIds",required=true) @ApiQueryParam(name="cartIds", description="购物车ID") String cartIds
	) throws Exception {
		service.addOrder(order, goodsList, cartIds);
		//保存订单商品信息
		return new View();
	}
	
	@ApiMethod(description="新增订单记录", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/order/add.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public View createOrder(@RequestBody @ApiBodyObject OrderForm form) throws Exception {
		
		//保存订单商品信息
		service.createOrder(form);
		
		return new View();
	}

}
